/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.threads.server.inGame;
import core.messages.server.inGame.Msg_Turn_Begin;
import core.threads.server.Server_Public_Data;
import core.threads.server.Server_Public_MsgBox;
import models.Player;

/**
 * 控制玩家轮次（开始）的线程
 * @author Invince
 */
public class T_Server_Turn_Controller  extends Thread{
/*----------Attributs-------------------*/
    /**
     * 用来辅助判断下一个玩家是谁
     */
    protected static int lastPlayerIndex=-1;
//    protected boolean stop;
//    protected boolean suspend;
/*----------Public functions------------*/
    @Override
    public void run() {
        int nextPlayerIndex=getNextPlayer();
        T_Server_Turn_Controller.lastPlayerIndex=nextPlayerIndex;
        Player player=Server_Public_Data.game.getListPlayer().get(nextPlayerIndex);
        Msg_Turn_Begin msgTurnBegin=new Msg_Turn_Begin("Server", player.getName());
        Server_Public_MsgBox.msgBox_Server_Send_Msg.offer(msgTurnBegin);
        Server_Public_MsgBox.refreshScreen();
    }


  
//    public void stopT(){
//        synchronized(this){
//            this.stop=true;
//        }
//    }
//
//    public void suspendT(){
//        synchronized(this){
//            this.suspend=true;
//        }
//    }
//
//    public void resumeT(){
//        synchronized(this){
//            this.suspend=false;
//        }
//    }

    /**
     * 获取下一个玩家的index
     * @return
     */
    public int getNextPlayer(){
        if(T_Server_Turn_Controller.getLastPlayerIndex()==-1){//Means the very beginning of the game
            return Server_Public_Data.game.getMajestyID();
        }else{
            int v=T_Server_Turn_Controller.getLastPlayerIndex();
            T_Server_Turn_Controller.setLastPlayerIndex(v+1);
            int index=T_Server_Turn_Controller.getLastPlayerIndex();
            if(index>=Server_Public_Data.game.getListPlayer().size()){
                index=0;
            }
            return index;
        }
    }
    
/*----------Private functions-----------*/
/*----------Constructor-----------------*/
    public T_Server_Turn_Controller() {
//        synchronized(this){
//            this.stop=false;
//            this.suspend=false;
//        }
    }
/*----------Gets and sets---------------*/
    public static int getLastPlayerIndex() {
        return lastPlayerIndex;
    }

    public static void setLastPlayerIndex(int lastPlayerIndex) {
        T_Server_Turn_Controller.lastPlayerIndex = lastPlayerIndex;
    }

}
